package yx.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsInvitationReply;
import yx.vo.DatagridVO;
import yx.vo.InvitationReplyVO;

@Service("invitationReplyService")
public class InvitationReplyServiceImpl implements InvitationReplyServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(InvitationReplyServiceImpl.class);
	
	private BaseDaoInt<BsInvitationReply> invitationReplyDao;

	public BaseDaoInt<BsInvitationReply> getInvitationReplyDao() {
		return invitationReplyDao;
	}

	@Autowired
	public void setInvitationReplyDao(BaseDaoInt<BsInvitationReply> invitationReplyDao) {
		this.invitationReplyDao = invitationReplyDao;
	}

	@Override
	public InvitationReplyVO save(InvitationReplyVO invitationReply) {
		Date d = new Date();
		Timestamp ts = new Timestamp(d.getTime());
		BsInvitationReply t = new BsInvitationReply();
		BeanUtils.copyProperties(invitationReply, t);
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("invitationId", invitationReply.getInvitationId());
		String hql = "select count(t.id) from BsInvitationReply t where t.invitationId = :invitationId";
		Long serialNo = invitationReplyDao.count(hql, params);
		t.setSerialNo(serialNo.intValue()+1);
		t.setReplyTime(ts);
		invitationReplyDao.save(t);
		BeanUtils.copyProperties(t, invitationReply);
		return invitationReply;
	}

	@Override
	public DatagridVO datagrid(InvitationReplyVO invitationReply) {
		DatagridVO dg = new DatagridVO();
		String hql = "from BsInvitationReply t where 1 = 1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(invitationReply, hql, params);
		String totalHql = "select count(*) "+hql;
		hql = addOrder(invitationReply, hql);
		List<BsInvitationReply> l = invitationReplyDao.find(hql,params,invitationReply.getPage(),invitationReply.getRows());
		List<InvitationReplyVO> nl = new ArrayList<InvitationReplyVO>();
		changeModel(l, nl);
		dg.setTotal(invitationReplyDao.count(totalHql,params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List<BsInvitationReply> l, List<InvitationReplyVO> nl) {
		if(l != null && l.size() > 0){
			for(BsInvitationReply t : l){
				InvitationReplyVO u = new InvitationReplyVO();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
	}

	private String addOrder(InvitationReplyVO invitationReply, String hql) {
		if(invitationReply.getSort() != null || invitationReply.getOrder() != null){
			hql += " order by "+invitationReply.getSort()+" " + invitationReply.getOrder();
		}
		return hql;
	}

	private String addWhere(InvitationReplyVO invitationReply, String hql, Map<String, Object> params) {
		return hql;
	}

	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete BsInvitationReply t where t.id in (";
		for(int i=0;i<nids.length;i++){
			if(i>0){
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		invitationReplyDao.executeHql(hql);
	}
	
	@Override
	public InvitationReplyVO edit(InvitationReplyVO invitationReply) {
		BsInvitationReply t = invitationReplyDao.get(BsInvitationReply.class, invitationReply.getId());
		BeanUtils.copyProperties(invitationReply, t);
		return invitationReply;
	}

}
